package bb.lanxing.model.database;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.LitePal;
import org.litepal.annotation.Column;

import com.alibaba.fastjson.annotation.JSONField;

import android.text.TextUtils;

import androidx.room.Ignore;

import bb.lanxing.common.config.Constants;
import bb.lanxing.common.config.ImagePostfix;
import bb.lanxing.model.json.UserAvatarMedal;
import bb.lanxing.model.json.UserSettings;
import bb.lanxing.util.JsonUtil;

public class User extends LitePalTableBase {
    @Column
    private int age;

    private long birthday;
    private String city;
    private long coin;
    private int cycleMonthCityRank;
    private double cycleMonthValidDistance;
    private long cycleMonthValidDuration;

    private String email;
    private String enuid;

    @Column
    private int gender = 1;
    @Column
    private int havePassword;
    @Column
    private int height;
    private int hots;
    private int hotsMonth;

    private boolean isCouponNew;
    private int level;

    private String mainTeam;

    private Medal medal;
    private String medalSmall;

    @JSONField(name = "username")
    private String name;


    @JSONField(name = "password")
    private String password;
    @Column
    private String phone = "";
    private String photoUrl;
    private String plateNum;
    private float powerFTP;
    private float powerHRLT;
    private String proName;
    private String province;

    @Column
    private String qq;
    private String qqAuth;
    private int rank;

    @Column
    private String recentHotList;

    private int runMonthCityRank;
    private double runMonthValidDistance;
    private long runMonthValidDuration;


    @Column
    private int score;

    private double totalDistance;
    private int uid;

    private List<UserAvatarMedal> userAvatarMedals;
    @Ignore

    private int walkMonthCityRank;
    private double walkMonthValidDistance;
    private long walkMonthValidDuration;

    private String wechat;
    private String weibo;
    private String tencentIm;
    @Column
    private double weight;

    private String nickName;
    private UserSettings userSettings;
    private String userSettingsJson;

    public User() {
    }

    public static User getUserByUid(long j) {
        String uid = String.valueOf(j);
        return LitePal.where("uid=?", uid).findFirst(User.class);
    }

    public void update(JSONObject node) throws JSONException {
        try {
            int code = node.getInt("code");
            if (code == 200) {
                JSONObject object = node.getJSONObject("data");
                long uid = object.getLong("userid");
                setUid((int) uid);
                String userName = object.optString("username");
                String qq = object.optString("qq");
                String wechat = object.optString("wechat");
                String weibo = object.optString("weibo");
                String phone = object.optString("phone");
                int level = object.optInt("level");
                int score = object.optInt("score", -1);
                int enuid = object.optInt("enuid");
                String recentHots = object.optString("recentHots");
                String tencentIm = object.optString("tmRegister");

                ////////////////
                double cycleMonthValidDistance = object.optDouble("cycleMonthValidDistance");
                long cycleMonthValidDuration = object.optLong("cycleMonthValidDuration");
                int cycleMonthCityRank = object.optInt("cycleMonthCityRank");
                double walkMonthValidDistance = object.optDouble("walkMonthValidDistance");
                long walkMonthValidDuration = object.optLong("walkMonthValidDuration");
                int walkMonthCityRank = object.optInt("walkMonthCityRank");
                double runMonthValidDistance = object.optDouble("runMonthValidDistance");
                long runMonthValidDuration = object.optLong("runMonthValidDuration");
                int runMonthCityRank = object.optInt("runMonthCityRank");
                setCycleMonthValidDistance(cycleMonthValidDistance);
                setCycleMonthValidDuration(cycleMonthValidDuration);
                setCycleMonthCityRank(cycleMonthCityRank);
                setWalkMonthValidDistance(walkMonthValidDistance);
                setWalkMonthValidDuration(walkMonthValidDuration);
                setWalkMonthCityRank(walkMonthCityRank);
                setRunMonthValidDistance(runMonthValidDistance);
                setRunMonthValidDuration(runMonthValidDuration);
                setRunMonthCityRank(runMonthCityRank);

                setRecentHotList(recentHots);
                ////////////////

                String avatar = object.optString("avatar");

                if (!TextUtils.isEmpty(tencentIm)) {
                    this.tencentIm = tencentIm;
                }

                if (!TextUtils.isEmpty(avatar)) {
                    setPhotoUrl(avatar);
                }
                if (!TextUtils.isEmpty(phone)) {
                    setPhone(phone);
                }
                if (!TextUtils.isEmpty(userName)) {
                    setName(userName);
                }
                if (!TextUtils.isEmpty(qq)) {
                    setQq(qq);
                } else {
                    setQq("");
                }
                if (!TextUtils.isEmpty(wechat)) {
                    setWechat(wechat);
                } else {
                    setWechat("");
                }
                if (!TextUtils.isEmpty(weibo)) {
                    setWeibo(weibo);
                } else {
                    setWeibo("");
                }
                if (0 != level) {
                    setLevel(level);
                }
                if (-1 != score) {
                    setScore(score);
                }
                if (0 != enuid) {
                    setEnuid(String.valueOf(enuid));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public double getTotalDistance() {
        return totalDistance;
    }

    public void setTotalDistance(double totalDistance) {
        this.totalDistance = totalDistance;
    }

    public List<UserAvatarMedal> getUserAvatarMedals() {
        return userAvatarMedals;
    }

    public void setUserAvatarMedals(List<UserAvatarMedal> userAvatarMedals) {
        this.userAvatarMedals = userAvatarMedals;
    }

    public String getLicenseNumberColor() {
        // TODO, hu, debug-Pro function
        return "#fff48fb1";// md_pink_200
        // return licenseNumberColor;
    }

    public String getLicenseNumberSkin() {
        // TODO, hu, debug-Pro function
        return "pro0100001";
        // return licenseNumberSkin;
    }

    public String getMainTeam() {
        return mainTeam;
    }

    public void setMainTeam(String mainTeam) {
        this.mainTeam = mainTeam;
    }

    public void setCouponNew(boolean couponNew) {
        isCouponNew = couponNew;
    }

    public int getUid() {

        return this.uid;
    }

    // temply method, use phone number instead, use user_id from server in the future
    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getHavePassword() {
        return havePassword;
    }

    public void setHavePassword(int havePassword) {
        this.havePassword = havePassword;
    }

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    public String getName() {
        if (!TextUtils.isEmpty(nickName)) {
            return nickName;
        }
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getScore() {
        return this.score;
    }

    public void setScore(int i) {
        this.score = i;
    }

    public long getBirthday() {
        return this.birthday;
    }

    public void setBirthday(long j) {
        this.birthday = j;
    }

    public int getGender() {
        return this.gender;
    }

    public void setGender(int i) {
        this.gender = i;
    }

    public double getWeight() {
        return this.weight;
    }

    public void setWeight(double d) {
        this.weight = d;
    }

    public int getAge() {
        return this.age;
    }

    public void setAge(int i) {
        this.age = i;
    }

    public String getRecentHotList() {
        return recentHotList;
    }

    public void setRecentHotList(String recentHotList) {
        this.recentHotList = recentHotList;
    }

    public Medal getMedal() {
        return medal;
    }

    public void setMedal(Medal medal) {
        this.medal = medal;
    }

    public int getHeight() {
        return this.height;
    }

    public void setHeight(int i) {
        this.height = i;
    }

    public int getLevel() {
        return this.level;
    }

    public void setLevel(int i) {
        this.level = i;
    }

    public String getMedalSmall() {
        return this.medalSmall;
    }

    public void setMedalSmall(String str) {
        this.medalSmall = str;
    }

    public String getProName() {
        // TODO, hu, debug-Pro function
        return "";
        // return this.proName;
    }

    public void setProName(String str) {
        this.proName = str;
    }

    public String getWechat() {
        return this.wechat;
    }

    public void setWechat(String str) {
        this.wechat = str;
    }

    public double getCycleMonthValidDistance() {
        return cycleMonthValidDistance;
    }

    public void setCycleMonthValidDistance(double cycleMonthValidDistance) {
        this.cycleMonthValidDistance = cycleMonthValidDistance;
    }

    public int getCycleMonthCityRank() {
        return cycleMonthCityRank;
    }

    public void setCycleMonthCityRank(int cycleMonthCityRank) {
        this.cycleMonthCityRank = cycleMonthCityRank;
    }

    public int getHots() {
        return hots;
    }

    public void setHots(int hots) {
        this.hots = hots;
    }

    public int getHotsMonth() {
        return hotsMonth;
    }

    public void setHotsMonth(int hotsMonth) {
        this.hotsMonth = hotsMonth;
    }

    public long getCycleMonthValidDuration() {
        return cycleMonthValidDuration;
    }

    public void setCycleMonthValidDuration(long cycleMonthValidDuration) {
        this.cycleMonthValidDuration = cycleMonthValidDuration;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getEnuid() {
        return enuid;
    }

    public void setEnuid(String enuid) {
        this.enuid = enuid;
    }

    public String getPlateNum() {
        // TODO, hu, debug-Pro function
        return "";// return empty to hide user plate
        // return plateNum;
    }

    public void setPlateNum(String plateNum) {
        this.plateNum = plateNum;
    }

    public double getRunMonthValidDistance() {
        return runMonthValidDistance;
    }

    public void setRunMonthValidDistance(double runMonthValidDistance) {
        this.runMonthValidDistance = runMonthValidDistance;
    }

    public double getWalkMonthValidDistance() {
        return walkMonthValidDistance;
    }

    public void setWalkMonthValidDistance(double walkMonthValidDistance) {
        this.walkMonthValidDistance = walkMonthValidDistance;
    }

    public float getPowerFTP() {
        return powerFTP;
    }

    public void setPowerFTP(float powerFTP) {
        this.powerFTP = powerFTP;
    }

    public float getPowerHRLT() {
        return powerHRLT;
    }

    public void setPowerHRLT(float powerHRLT) {
        this.powerHRLT = powerHRLT;
    }

    public int getRank() {
        return rank;
    }

    public void setRank(int rank) {
        this.rank = rank;
    }

    public int getRunMonthCityRank() {
        return runMonthCityRank;
    }

    public void setRunMonthCityRank(int runMonthCityRank) {
        this.runMonthCityRank = runMonthCityRank;
    }

    public int getWalkMonthCityRank() {
        return walkMonthCityRank;
    }

    public void setWalkMonthCityRank(int walkMonthCityRank) {
        this.walkMonthCityRank = walkMonthCityRank;
    }

    public long getRunMonthValidDuration() {
        return runMonthValidDuration;
    }

    public void setRunMonthValidDuration(long runMonthValidDuration) {
        this.runMonthValidDuration = runMonthValidDuration;
    }

    public long getWalkMonthValidDuration() {
        return walkMonthValidDuration;
    }

    public void setWalkMonthValidDuration(long walkMonthValidDuration) {
        this.walkMonthValidDuration = walkMonthValidDuration;
    }

    public String getPhotoUrl() {
        return photoUrl;
    }

    public void setPhotoUrl(String photoUrl) {
        this.photoUrl = photoUrl;
    }

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getQqAuth() {
        return qqAuth;
    }

    public void setQqAuth(String qqAuth) {
        this.qqAuth = qqAuth;
    }

    public String getWeibo() {
        return this.weibo;
    }

    public void setWeibo(String str) {
        this.weibo = str;
    }

    public String getBigPhoto() {
        if (!TextUtils.isEmpty(this.photoUrl) && this.photoUrl.startsWith(Constants.UPYUN_HOST)
                && !this.photoUrl.endsWith(Constants.UPYUN_IMAGE_TYPE_PHOTO)) {
            return this.photoUrl + ImagePostfix.UPYUN_IMAGE_TYPE_AVATAR_BIG;
        }
        return this.photoUrl;
    }

    public UserSettings getUserSettings() {
        String str;
        if (this.userSettings == null && (str = this.userSettingsJson) != null) {
            this.userSettings = paseUserSettings(str);
        }
        return this.userSettings;
    }

    private UserSettings paseUserSettings(String str) {
        UserSettings userSettings = null;
        if (str == null) {
            return null;
        }
        try {
            JSONObject jSONObject = new JSONObject(str);
            String stringValue = JsonUtil.getStringValue("segments_hr", jSONObject);
            String stringValue2 = JsonUtil.getStringValue("segments_ca", jSONObject);
            float doubleValue = (float) JsonUtil.getDoubleValue("lthr", jSONObject);
            float doubleValue2 = (float) JsonUtil.getDoubleValue("ftp", jSONObject);
            UserSettings userSettings2 = new UserSettings();
            userSettings2.setSegmentsHr(stringValue);
            userSettings2.setSegmentsCa(stringValue2);
            userSettings2.setLthr(doubleValue);
            userSettings2.setFtp(doubleValue2);
            return userSettings2;
        } catch (JSONException err) {
            err.printStackTrace();

        }
        return userSettings;
    }

    public String getTencentIm() {
        return tencentIm;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
}
